import {EventDispatcher,Utils} from './common.js'
import DataStorage from "./data-storage.js"
import UIBreadcrumb from "./ui/breadcrumb-ui.js"
import D from "./delegator.js"

export default class Breadcrumb extends EventDispatcher {
    constructor() {
        super();
        this._ui = new UIBreadcrumb();

        DataStorage.RegistListener(DataStorage.RegistListener.EVT_DATASTORAGE_ARTICLE_FETCHED, (params) => {
            const _articleData = params[0];
            let _metaData = DataStorage.GetPageProperty(_articleData.SUID);
            const _arr = [];
            while(_metaData) {
                _arr.push(_metaData);
                _metaData = DataStorage.GetPageProperty(_metaData.PSUID);
            }
            this._ui.CreateContent(_arr);
        });

        this._ui.liContainerElem.addEventListener('click', (e) => {
            if (D(e.target).HasAttribute('data-slog-SUID')) {
                const _SUID = D.GetDatasetValue('slogSuid');
                this._event.DispatchEvent(Breadcrumb.EVT_BREADCRUMB_CLICKED, _SUID);
            }
        });
    }
    get layoutElem() {return this._ui.element;}

}

Utils.DefineFixedProperty(Breadcrumb, 'EVT_BREADCRUMB_CLICKED', Symbol());

